<?php defined('BASEPATH') OR exit('No direct script access allowed');

    // This can be removed if you use __autoload() in config.php OR use Modular Extensions
    require APPPATH.'/libraries/REST_Controller.php';
    class Rent extends CI_Controller
    {
        
         public function __construct()
        {
            parent::__construct();
            if(!class_exists("translate"))
                $this->translate=& $this->factory->create('translate', 'foo', 'bar');

        }


        function available_cars() {

            $dateTimeFrom = strtotime('11.04.2013');
            $dateTimeTo = strtotime('12.04.2013');
            $days = calculateDays($dateTimeFrom, $dateTimeTo, FALSE);

            /*$dateTimeFrom = strtotime($this->get('dateTimeFrom'));
            $dateTimeTo = strtotime($this->get('dateTimeTo'));
            $days = calculateDays($dateTimeFrom, $dateTimeTo, FALSE);*/

            /*$dateTimeFrom = strtotime('18.02.2012 08:00:00');
            $dateTimeTo = strtotime('19.02.2012 08:00:00');*/

            if(!$dateTimeFrom || !$dateTimeTo) {

                print_r(array('info'=>'Enter dates:'), 200); // 200 being the HTTP response code
                return FALSE;
            }

            if($dateTimeFrom >= $dateTimeTo) {

                print_r(array('info'=>'Wrong date.'), 200); // 200 being the HTTP response code
                return FALSE;
            }

            /*if($days > 15) {

            $this->response(array('info'=>'Max 15 days.'), 200); // 200 being the HTTP response code
            return FALSE;
            } */

            $dbRet = $this->getAvailable($dateTimeFrom, $dateTimeTo);
            
            $lang = $this->input->get_post('lang');
            //$dbRet = $this->translate->res_array($dbRet, $lang);
            
            
            print_r($dbRet);

        }


        function getAvailable($datetimefrom,$datetimeto) {

            $query = "select * from carbooking
            where ((datefrom between ".$datetimefrom." and ".$datetimeto.")
            or (dateto between ".$datetimefrom." and ".$datetimeto.")
            or((".$datetimefrom." > datefrom) and (".$datetimefrom." < dateto) or (".$datetimeto." > datefrom and ".$datetimeto." < dateto)))
            and (status = 1 or status = 3 or status = 0) and user_id = 7
            group by carid order by carid desc";

            $notavailable = array();

            $q = $this->db->query($query);

            foreach($this->db->query($query)->result_array() as $one) {
                $notavailable[] = $one['carid'];
            }

            //      now list available cars
            if(count($notavailable) > 0) {
                $nstr = implode(',', $notavailable);
                $q = $this->db->query("

                    SELECT car.*, gallery.path as g_path, pictures.filename as f_name
                    FROM car
                    LEFT JOIN gallery ON car.id = gallery.`posts_ID`
                    LEFT JOIN pictures ON gallery.ID = pictures.gallery_ID
                    AND pictures.ID=
                    (
                    SELECT pictures.ID
                    FROM pictures
                    WHERE pictures.gallery_ID = gallery.ID
                    ORDER BY pictures.order ASC, pictures.ID ASC
                    LIMIT 1, 1  
                    )
                    where car.id not in (".$nstr.") and (user_id = 7) order by day13price asc
                ");

            }else {
                $q = $this->db->query("
                    SELECT car.*, gallery.path as g_path, pictures.filename as f_name
                    FROM car
                    LEFT JOIN gallery ON car.id = gallery.`posts_ID`
                    LEFT JOIN pictures ON gallery.ID = pictures.gallery_ID
                    AND pictures.ID=
                    (
                    SELECT pictures.ID
                    FROM pictures
                    WHERE pictures.gallery_ID = gallery.ID
                    ORDER BY pictures.order ASC, pictures.ID ASC
                    LIMIT 1, 1  
                    )
                    where user_id = 7 order by day13price asc
                ");
            }

            return $q;
        }

       

    }
